package oa.hleast.xswl.controller.app.chat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.annotation.UserAuth;
import oa.hleast.xswl.common.constant.ChatDateEnum;
import oa.hleast.xswl.pojo.domain.chat.CmsChatMessageLog;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.chat.ChatForm;
import oa.hleast.xswl.pojo.vo.chat.CmsChatMessageLogVo;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.chat.IChatService;
import oa.hleast.xswl.service.chat.impl.WebSocketClientService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 302
 */
@Api(tags = "【移动端】智能客服")
@RestController
@RequestMapping("/api.app/v1/chat")
@Slf4j
@AllArgsConstructor
public class ChatController {

    private final IChatService iChatService;
    private final WebSocketClientService webSocketClientService;
    private final IUmsUserService umsUserService;


    @ApiOperation(value = "聊天记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("/findChatMessageLog")
    private Result<List<CmsChatMessageLogVo>> findChatMessageLog(@RequestParam @NotNull Integer page, @RequestParam @NotNull Integer limit) {
        Long userId = RequestUtils.getUserId();
        log.info("userId = {},umsUserService = {}",userId,umsUserService);
        UmsUserDto userResult = umsUserService.getUserDtoById(userId);
        if (ObjectUtils.isEmpty(userResult) ||  ObjectUtils.isEmpty(userResult.getId())) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        Page<CmsChatMessageLogVo> result = iChatService.findChatMessageLog(userId, page, limit);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "发送语音聊天", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "ChatForm", dataTypeClass = ChatForm.class)
    @PostMapping("/chat")
    private Result<String> chat(ChatForm form) {

        log.info("form = {}", form);
        try {
            /* @RequestBody 会被作为application/json 格式　，这里不加　以form-data 形式接受file */
            Long userId = RequestUtils.getUserId();
            UmsUserDto userResult = umsUserService.getUserDtoById(userId);
            if (ObjectUtils.isEmpty(userResult) ||  ObjectUtils.isEmpty(userResult.getId())) {
                return Result.failed(ResultCode.USER_NOT_EXIST);
            }
            String url = webSocketClientService.handleVoice(userId, form);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }

    }

    @ApiOperation(value = "发送文字聊天", httpMethod = "GET")
    @ApiImplicitParam(name = "distinguishValue", value = "聊天文字", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @GetMapping("/chatByTxt")
    private Result<CmsChatMessageLog> chatByTxt(@RequestParam @NotBlank String distinguishValue) {

        Long userId = RequestUtils.getUserId();
        UmsUserDto userResult = umsUserService.getUserDtoById(userId);
        if (ObjectUtils.isEmpty(userResult) ||  ObjectUtils.isEmpty(userResult.getId())) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        ChatForm form = ChatForm.builder().distinguishValue(distinguishValue).type(ChatDateEnum.TXT.name()).build();
        CmsChatMessageLog chat = webSocketClientService.chat(userId, form, false);

        return Result.success(chat);
    }

}
